package org.huawei260.TiKu.ODsuanfa;

import java.util.Arrays;
import java.util.Scanner;
public class E_2024E_3201__不定滑窗_最长和为目标值的子序列 {

//	public class Main {
		public static void main(String[] args) {
			Scanner sc = new Scanner(System.in);

			int[] nums = Arrays.stream(sc.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
			int target = Integer.parseInt(sc.nextLine());

			System.out.println(getResult(nums, target));
		}

		public static int getResult(int[] nums, int target) {
			int n = nums.length;

			// 前缀和数组
			int[] preSum = new int[n + 1];
			for (int i = 1; i <= n; i++) {
				preSum[i] = preSum[i - 1] + nums[i - 1];
			}

			// 记录最长连续子序列的长度
			int maxLen = -1;

			// 求解所有连续子序列的区间和
			for (int l = 0; l <= n - 1; l++) {
				for (int r = l + 1; r <= n; r++) {
					if (preSum[r] - preSum[l] == target) {
						maxLen = Math.max(maxLen, r - l);
					}
				}
			}

			return maxLen;
		}
	}